package util

import (
	"bufio"
	"io"
	"os/exec"
	"strings"
	"wiliwili/model"
)

func ExecCommand(commandName string, params []string, resourcesId uint) error {
	cmd := exec.Command(commandName, params...)

	//显示运行的命令
	LogD("cmd.Args", cmd.Args)

	stdout, err := cmd.StdoutPipe()

	if err != nil {
		LogD("err", err)
		return err
	}

	cmd.Start()

	reader := bufio.NewReader(stdout)

	// 实时循环读取输出流中的一行内容
	for {
		line, err2 := reader.ReadString('\n')
		LogD("line2222", line)
		if err2 != nil || io.EOF == err2 {
			LogD("err2", err2)
			break
		}
		line = strings.Replace(line, "\n", "", -1)

		// 根据命令，处理结果
		switch commandName {
		case model.Test:
			model.AddLotusClient(resourcesId, commandName, line, 1)
		case model.LCImport:
			model.AddLotusClient(resourcesId, commandName, line, 1)
		}
		LogD("line", line)
	}

	cmd.Wait()
	return nil
}
